package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.entity.JsContents;
import com.cxf.windex.entity.Templates;
import com.cxf.windex.mapper.TemplatesMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 模板Biz
 *
 * @author chenxf
 * @version 1.0
 * @date 2018-12-24 15:00
 **/
@Service
public class TemplatesBiz extends BaseBiz<TemplatesMapper, Templates> {

    /**
     * 模板列表
     *
     * @param limit 数量
     * @param offset 页码
     * @param orderStr  排序(升序或者降序)
     * @param title 标题
     * @param type 类型
     * @param range 0商家 1村长
     * @return
     */
    public List<Templates> list(int limit, int offset, String orderStr, String title, String type, String range) {
        Example example = new Example(Templates.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(title)){
            criteria.andLike("title",  "%"+title+"%" );
        }
        criteria .andEqualTo("ranges", range);
        if(StringUtils.isNotBlank(type)){
            criteria .andEqualTo("type", type);
        }
        example.setOrderByClause("order_num asc,crt_time " + orderStr);
        PageHelper.startPage(offset, limit);
        return selectByExample(example);
    }

    /**
     * 保存模板
     * @param entity 模板信息
     */
    public void save(Templates entity){
        if(entity.getId()==null||entity.getId()==0){
            insertSelective(entity);
        }else  {
            updateSelectiveById(entity);
        }
    }
}
